package builders;

import backbone.IBuildable;
import backbone.IForceable;
import backbone.IPaintable;
import backbone.IUpdatable;
import entities.FixedMass;
import entities.Mass;
import entities.PointObject;
import force.GlobalForce;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import main.Simulation;


/**
 * Builds Point Objects and adds them to simulation.
 * @author Paul
 * @author Lance
 * 
 */
public class PointObjectBuilder implements IBuildable {
    
    private List<PointObject> myPointObjects;
    
    /**
     * Instantiates the class!
     */
    public PointObjectBuilder () {
        myPointObjects = new ArrayList<PointObject>();  
    }
    
    /**
     * Provides the PointObject with a GlobalForce and registers the
     * PointObject with that GlobalForce.
     */
    @Override
    public void build(Simulation sim, LinkedList<Double> properties) {
        
        IUpdatable updatable;
        IPaintable paintable;
       
        int id = (int)(double)properties.poll();
        double x = properties.poll();        
        double y = properties.poll();
        double mass = properties.poll();
        if (mass > 0) {
            updatable = new Mass(id, x, y, mass);
            sim.addUpdatable(updatable);
            sim.addForceable((IForceable) updatable);
            for (GlobalForce f : sim.getForces()) {
                f.add((IForceable) updatable);
            }
            paintable = (IPaintable) updatable;
        }
        else {
            // If mass is indicated as negative in datafile, create FixedMass.
            paintable = new FixedMass(id, x, y);
        }
        sim.addPaintable(paintable);
        sim.addPointObject((PointObject) paintable);
        myPointObjects.add((PointObject) paintable);
    }
    
    /**
     * @param id returns point object with this ID
     * @return
     */
    public PointObject getPointObject (int id) {
        for (int i = myPointObjects.size() - 1; i >= 0; i--) {
            if (myPointObjects.get(i).match(id)) {
                return myPointObjects.get(i);
            }
        }
        return null;
    }
    
    public void add(PointObject p) {
        myPointObjects.add(p);
    }
}
